package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.*;
import com.qf.service.*;
import com.qf.util.JacksonUtil;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import com.qf.vo.GoodsAllinone;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@CrossOrigin
@RequestMapping("/admin/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private GoodsAttributeService goodsAttributeService;
    @Autowired
    private GoodsSpecificationService goodsSpecificationService;
    @Autowired
    private GoodsProductService goodsProductService;


    //http://localhost:8080/admin/goods/list?
    //page=1&limit=20&goodsSn=1&name=aa&sort=add_time&order=desc
    @RequiresPermissions("admin:goods:list")
    @RequiresPermissionsDesc(menu = {"商品管理","商品列表"},button = "查询")
    @GetMapping("/list")
    public Object list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit,
                       String goodsSn,String name,String sort,String order) {

        IPage<DtsGoods> iPage = goodsService.findListByQuery(page,limit,goodsSn,name,sort,order);

        Map data=new HashMap();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());
        return ResponseUtil.ok(data);
    }

    @GetMapping("/catAndBrand")
    public Object catAndBrand(){
        List<CatVo> catVoList=categoryService.findAll();

        List<CatVo> brandList=brandService.findAll();

        Map data=new HashMap();
        data.put("categoryList",catVoList);
        data.put("brandList",brandList);

        return ResponseUtil.ok(data);
    }

    //http://localhost:8080/admin/goods/detail?id=23861
    @GetMapping("/detail")
    public Object detail(Integer id){
        List<Integer> categoryIds = goodsService.findGoodsCategory(id);
        DtsGoods dtsGoods = goodsService.findOneGoods(id);
        List<DtsGoodsAttribute> attributes = goodsAttributeService.findListByGoodsId(id);
        List<DtsGoodsSpecification> specifications = goodsSpecificationService.findListByGoodsId(id);
        List<DtsGoodsProduct> products = goodsProductService.findListByGoodsId(id);

        Map data=new HashMap();
        data.put("categoryIds",categoryIds);
        data.put("goods",dtsGoods);
        data.put("attributes",attributes);
        data.put("specifications",specifications);
        data.put("products",products);
        return ResponseUtil.ok(data);
    }

    //http://localhost:8080/admin/goods/update
    @RequiresPermissions("admin:goods:update")
    @RequiresPermissionsDesc(menu = {"商品管理","商品列表"},button = "更新")
    @PostMapping("/update")
    public Object update(@RequestBody GoodsAllinone goodsAllinone){
        goodsService.updateAll(goodsAllinone);

        return ResponseUtil.ok();
    }
    @RequiresPermissions("admin:goods:create")
    @RequiresPermissionsDesc(menu = {"商品管理","商品列表"},button = "上架")
    @PostMapping("/create")
    public Object create(@RequestBody GoodsAllinone goodsAllinone){
        goodsService.createGoods(goodsAllinone);

        return ResponseUtil.ok();
    }
    @RequiresPermissions("admin:goods:delete")
    @RequiresPermissionsDesc(menu = {"商品管理","商品列表"},button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsGoods dtsGoods){
        goodsService.deleteGoods(dtsGoods);

        return ResponseUtil.ok();
    }


}
